﻿<UserControl x:Class="NDasm.Core.UI.MethodDasm.MethodView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="root">
    <UserControl.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontFamily" Value="Consolas" />
            <Setter Property="FontSize" Value="14" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>         
        <Border Grid.Row="0" Background="CornflowerBlue" Padding="3">
            <StackPanel>
                <TextBlock Text="{Binding MethodName}" Foreground="White" FontSize="20" FontWeight="Bold" />
                <TextBlock Text="{Binding FullMethodName}" Foreground="White" FontSize="12"/>
            </StackPanel>            
        </Border>
        <ListBox Grid.Row="1" ItemsSource="{Binding Instructions}" SelectedItem="{Binding SelectedInstruction}" Grid.IsSharedSizeScope="True"
                 SelectionChanged="InstructionSelected">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">                                
                                <Border x:Name="border" BorderThickness="1" SnapsToDevicePixels="True">
                                    <ContentPresenter />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="border" Property="BorderBrush" Value="Gray" />                                                                                    
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}" />                                                    
                    </DataTemplate.Resources>
                    <Grid>                        
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="A"/>
                            <ColumnDefinition Width="8"/>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="{Binding Offset}" Foreground="DarkGray"/>
                        <Grid Grid.Column="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="8"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding OpCode, Mode=OneWay}" Foreground="Blue" Grid.Column="0"/>
                            <TextBlock Text="{Binding Operand, Mode=OneWay}" Grid.Column="2">
                                <TextBlock.Style>
                                    <Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="{x:Type TextBlock}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsJumpInstruction}" Value="True">
                                                <Setter Property="Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <Button Grid.Column="2" Cursor="Hand" Command="{Binding DataContext.GoToCommand, ElementName=root}" CommandParameter="{Binding JumpOffset}">
                                <Button.Style>
                                    <Style>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsJumpInstruction}" Value="False">
                                                <Setter Property="Button.Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Button.Style>
                                <Button.Template>
                                    <ControlTemplate>
                                        <ControlTemplate.Resources>
                                            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}" />
                                        </ControlTemplate.Resources>
                                        <TextBlock Foreground="DarkBlue">
                                            <Underline>
                                                <Underline.Inlines>
                                                    <Run Text="{Binding Operand, Mode=OneWay}"/>
                                                </Underline.Inlines>
                                            </Underline>
                                        </TextBlock>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </Grid>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>
